﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Licence__WCG.Models;

namespace Licence__WCG.Controllers
{
    public class HomeController : Controller
    {
        // 实例化上下文类
        Model1 model = new Model1();
        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 登录页面数据处理
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="Pwd">密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(string UserName="",string Pwd="")
        {
            // 判断验证
            if (ModelState.IsValid)
            {
                //查询输入结果
                var list = model.UserInfo.FirstOrDefault(p=>p.UserName==UserName&&p.Pwd==Pwd);
                if (list!=null)
                {
                    //登录成功获取用户ID、进入Index页面
                    Session["ID"] = list.ID;
                    return RedirectToAction("Index");

                }
                else
                {
                    //登录失败提示错误
                    ViewBag.IsError = true;
                    return View();
                }

            }
            else
            {
                //登录失败提示错误
                ViewBag.IsError = true;
                return View();
            }
        }
        /// <summary>
        /// 主页列表页面
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="IsReview"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public ActionResult Index(string Name = "", string IsReview = "", string State = "") 
        {
            var list = model.Licence.Where(p => p.Name.Contains(Name)).ToList();
            if (!string.IsNullOrEmpty(IsReview))
            {
                if (IsReview == "1")
                { 
                    list = list.Where(u => u.IsReview).ToList();
                }
                else
                {
                    list = list.Where(u => u.IsReview==false).ToList();
                }
            }
            if (!string.IsNullOrEmpty(State))
            {
                list = list.Where(u => u.State == State).ToList();
            }

            List<SelectListItem> isReviewList= new List<SelectListItem>();
            isReviewList.Add(new SelectListItem() { Value = "", Text = "--请选择--" });
            isReviewList.Add(new SelectListItem() { Value = "0", Text = "是" });
            isReviewList.Add(new SelectListItem() { Value = "1", Text = "否" });
            ViewBag.isReviewList = new SelectList(isReviewList, "Value", "Text");

            List<SelectListItem> stateList = new List<SelectListItem>();
            stateList.Add(new SelectListItem() { Value = "", Text = "--请选择--" });
            stateList.Add(new SelectListItem() { Value = "借出", Text = "借出" });
            stateList.Add(new SelectListItem() { Value = "在库", Text = "在库" });
            ViewBag.stateList = new SelectList(stateList, "Value", "Text");

            return View(list);
        
        }
        public ActionResult Create()
        {
            List<SelectListItem> stateList = new List<SelectListItem>();
            stateList.Add(new SelectListItem() { Value = "", Text = "--请选择--" });
            stateList.Add(new SelectListItem() { Value = "借出", Text = "借出" });
            stateList.Add(new SelectListItem() { Value = "在库", Text = "在库" });
            ViewBag.stateList = new SelectList(stateList, "Value", "Text");
            return View();
        }
        [HttpPost]
        public ActionResult Create(Licence licence)
        {

                model.Licence.Add(licence);
                int res= model.SaveChanges();
                if (res>0)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return Content("<script >alert('添加失败');</script >", "text/html");
                }
        }
        [HttpPost]
        public ActionResult Delete(int? id)
        {
            if (id==null)
            {
                // 如果id为空返回false
                return Json(false);
            }
            var list = model.Licence.FirstOrDefault(p=>p.ID==id);
            if (list == null)
            {
                // 如果list为空返回false
                return Json(false);
            }
            // 执行删除操作
            model.Licence.Remove(list);
            // 保存至数据库
            var res = model.SaveChanges() > 0;
            // 返回运行结果
            return Json(res);
        }
        [HttpPost]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                // 如果id为空返回false
                return Json(false);
            }
            var list = model.Licence.FirstOrDefault(p => p.ID == id);
            if (list == null)
            {
                // 如果list为空返回false
                return Json(false);
            }
            if ((int)Session["Id"]==list.ID)
            {
                list.State = "在库";
            }
            else
            {
                list.State = "借出";
            }

            // 保存至数据库
            var res = model.SaveChanges() > 0;
            // 返回运行结果
            return Json(res);
        }
    }
}